import pymysql

host='localhost'
port=3306
user='root'
password='123456'
database='mydb1'
charset='utf8'

db=pymysql.Connect(host=host,port=port,user=user,password=password,database=database,charset=charset)
print('数据库已连接')

#新增数据
id=1
name='whitecat'
sql=f'''insert into cat values('{id}','{name}')'''

cursor=db.cursor()
try:
    cursor.execute(sql)
    db.commit()
    print('数据添加成功')
except Exception as e:
    db.rollback()
    print(e)

